In [1]:
import numpy as np
import plotly.graph_objects as go
In [2]:
import altair as alt
alt.renderers.enable('default')
Out[2]:
RendererRegistry.enable('default')
In [3]:
RANDOM_SEED = 15
time = np.arange(4*365 + 1 ) # 4 years
In [4]:
time
Out[4]:
array([   0,    1,    2, ..., 1458, 1459, 1460])
In [5]:
def trend(time , slope = 0 ):
    return slope * time
In [6]:
def plot_series(time , series , mode = "lines" , start = 0, end = None , label = None , color = None,Title=None):
    
    fig = go.Figure()
    fig.add_trace(go.Scatter(x= time[start:end] , y= series[start:end], marker=dict(color=color)))
    fig.update_layout(
                        title=Title,
                        xaxis_title="Time",
                        yaxis_title="Value")


    fig.show()
In [7]:
slope= 0.1
series = trend(time,slope)
plot_series(time,series,Title='Trend plot 1',color='green')
In [8]:
slope= -1
series = trend(time,slope)
plot_series(time,series,Title='Trend plot 2',color='red')
In [9]:
def seasonal_pattern(season_time):
    return np.where(season_time<0.45,
                   np.cos(season_time* 2 *np.pi),
                   1 / np.exp(3*season_time))
In [10]:
def seasonality(time , period , amplitude = 1 , phase = 0 ):
    season_time = ((time + phase) %  period ) / period
    return amplitude *seasonal_pattern(season_time)
In [11]:
amplitude = 40
series = seasonality(time , period = 365, amplitude = amplitude , phase = 0 )
plot_series(time , series, color = "blue",Title="Seasonality Plot")
In [12]:
amplitude = 100
series = seasonality(time , period = 90, amplitude = amplitude , phase = 25 )
plot_series(time , series, color = "darkblue",Title="Seasonality Plot")
In [13]:
baseline = 10
slope = 0.08
amplitude = 40
series = baseline + trend(time , slope) + seasonality(time , period = 365 , amplitude= amplitude)
series
Out[13]:
array([ 50.        ,  50.07407357,  50.13629603, ..., 128.66448999,
       128.72791854, 166.8       ])
In [14]:
plot_series(time , series, color = "orange",Title="Trend & Seasonality")
In [15]:
def white_noise(time, noise_level = 1 , seed = None):
    random = np.random.RandomState(seed)
    return random.random(len(time)) * noise_level
In [16]:
noise_level = 10
noise = white_noise(time , noise_level , seed = RANDOM_SEED)
In [17]:
noise
Out[17]:
array([8.48817697, 1.78895925, 0.54363214, ..., 9.36769178, 1.29335202,
       7.45229414])
In [18]:
plot_series(time , noise, color = "yellow",Title="Noise")
In [19]:
plot_series(time[:400] , noise[:400], color = "yellow",Title="Noise")
In [20]:
series = baseline + trend(time , slope) + seasonality(time , period = 365 , amplitude= amplitude)
series += white_noise(time , noise_level = 10 , seed = RANDOM_SEED)
series
Out[20]:
array([ 58.48817697,  51.86303282,  50.67992817, ..., 138.03218177,
       130.02127055, 174.25229414])
In [21]:
plot_series(time , series, color = "purple",Title=" Noise + Seasonality + Trend Plot ")
In [22]:
def autocorrelation_1(time , amplitude , seed = None):
    rnd = np.random.RandomState(seed)
    a1 = 0.5
    a2 = -0.1
    rnd_ar = rnd.randn(len(time) + 50)
    rnd_ar[:50] = 100
    for step in range(50, len(time) + 50 ):
        rnd_ar[step] += a1 * rnd_ar[step - 50]
        rnd_ar[step] += a2 * rnd_ar[step - 33]
    return rnd_ar[50:] * amplitude
In [23]:
def autocorrelation_2(time, amplitude, seed=None):
    rnd = np.random.RandomState(seed)
    a1 = 0.8
    ar = rnd.randn(len(time) + 1)
    for step in range(1, len(time) + 1):
        ar[step] += a1 * ar[step - 1]
    return ar[1:] * amplitude
In [24]:
series = autocorrelation_1(time , amplitude , seed = RANDOM_SEED)
series
Out[24]:
array([1679.13089474, 1608.78752127, 1675.79279398, ...,   17.33105583,
         56.42678071,   43.13911093])
In [25]:
plot_series(time , series, color = "cyan",Title="Auto correlation plot-1")
In [26]:
plot_series(time[:400] , series[:400], color = "cyan",Title="Auto correlation plot-1")
In [27]:
series = autocorrelation_2(time , amplitude , seed = RANDOM_SEED)
series
Out[27]:
array([  3.57687683,  -3.37483989, -22.77145884, ...,  49.68885684,
        43.00255944,  70.47696582])
In [28]:
plot_series(time , series, color = "darkcyan",Title="Auto correlation plot-2")
In [29]:
plot_series(time[:400] , series[:400], color = "darkcyan",Title="Auto correlation plot-2")
In [30]:
amplitude = 10
slope = 2
series = autocorrelation_1(time ,amplitude , seed = RANDOM_SEED) + trend(time , slope)
series
Out[30]:
array([ 419.78272368,  404.19688032,  422.9481985 , ..., 2920.33276396,
       2932.10669518, 2930.78477773])
In [31]:
plot_series(time[:400] , series[:400], color = "grey",Title="Auto correlation + Trend plot")
In [32]:
amplitude = 10
slope = 2
series = autocorrelation_1(time , amplitude , seed = RANDOM_SEED ) + seasonality(time , period= 50 , amplitude = 150) + trend(time , slope)
series += white_noise(time , noise_level=100)
series
Out[32]:
array([ 626.52507791,  622.90848255,  663.18514082, ..., 3098.70907138,
       3016.86132096, 3016.99984585])
In [37]:
plot_series(time , series, color = "darkred",Title="Autocorrelation + Seasonality + Noise + Trend Plot")
In [38]:
plot_series(time[:400] , series[:400], color = "darkred",Title="Autocorrelation + Seasonality + Noise + Trend Plot")
In [39]:
amplitude1 = 10
amplitude2 = 5
slope1 = 2
slope2 = -2
series1 = autocorrelation_2(time, amplitude1, seed=RANDOM_SEED) + seasonality(time, period=50, amplitude=150) + trend(time, slope1)
series2 = autocorrelation_2(time, amplitude2, seed=RANDOM_SEED) + seasonality(time, period=50, amplitude=2) + trend(time, slope2) + 750+  white_noise(time , 30) 
series1[200:] = series2[200:]
series1 += white_noise(time, noise_level=100 , seed= RANDOM_SEED)
series1
Out[39]:
array([  235.77598893,   167.86308772,   149.03093089, ...,
       -2039.69623405, -2145.07515762, -2081.54100263])
In [52]:
def plot_series_breakpoint(time , series , mode = "lines" , start = 0, end = None , label = None , color = None,Title=None, Breakpoint=None):
    
    fig = go.Figure()
    fig.add_trace(go.Scatter(x= time[start:end] , y= series[start:end], marker=dict(color=color)))
    
    if Breakpoint!= None:
        fig.add_vline(x =Breakpoint["Breakpoint"] ,line_dash='dash', line_color=Breakpoint["color"])            
        
    fig.update_layout(
                        title=Title,
                        xaxis_title="Time",
                        yaxis_title="Value")
    
    


    fig.show()
In [55]:
plot_series_breakpoint(time[:400] , series[:400], color = "darkseagreen",Title="Autocorrelation + Seasonality + Noise + Trend Plot + Breakpoint",Breakpoint={"Breakpoint":200,"color":"deeppink"})
In [ ]: